﻿using AutoMapper;
using DFGK.IRepository;
using DFGK.IService;
using DFGK_WMS.Model.Models;
using NPOI.SS.Formula.Functions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DFGK.Service
{
    public class AdminConfigurationService : BaseService, IAdminConfigurationService
    {
        private readonly IAdminConfigurationRepository _adminConfigurationRepository;
        private readonly IUnitOfWork _unitOfWork;
        public AdminConfigurationService(IMapper mapper, IAdminConfigurationRepository _adminConfigurationRepository, IUnitOfWork _unitOfWork) : base(mapper)
        {
            this._adminConfigurationRepository = _adminConfigurationRepository;
            this._unitOfWork = _unitOfWork;
        }

        public bool AddAdminConfiguration(AdminConfiguration adminConfiguration)
        {
            _adminConfigurationRepository.AddEntity(adminConfiguration);
            return _unitOfWork.SaveChangesEntity();
        }

        public bool DelAdminConfiguration(long id)
        {
            var cust = _adminConfigurationRepository.QueryEntityById(id);
            if (cust !=null)
            {
                _adminConfigurationRepository.DelEntity(cust);
                return _unitOfWork.SaveChangesEntity();
            }
            return false;
        }

        public List<AdminConfiguration> GetAdminConfiguration(string? userAccount, int pageIndex, int pageSize, out int totalCount)
        {
            var custList = _adminConfigurationRepository.GetEntityAlls();
            if (!string.IsNullOrEmpty(userAccount))
            {
                custList = custList.Where(c => c.UserAccount.Contains(userAccount));
            }
            var list = _adminConfigurationRepository.GetEntityAlls(custList, pageIndex, pageSize, out totalCount, x => x.CreateDate, false);
            return list.ToList();
        }

        public AdminConfiguration? GetAdminConfigurationById(long id)
        {
            return _adminConfigurationRepository.QueryEntityById(id);
        }

        public bool UpdAdminConfiguration(AdminConfiguration AdminConfiguration)
        {
            var aobj = _adminConfigurationRepository.QueryIsEntityWhere(x => x.Id == AdminConfiguration.Id);
            if (aobj == null)
            {
                return false;
            }
            _adminConfigurationRepository.UpdEntity(AdminConfiguration);
            return _unitOfWork.SaveChangesEntity();
        }
    }
}
